#!/usr/bin/env bash
version="v1.4"

gurhelp(){
    echo "\
GUR $version -GPL v3
Usage: gur [-h] [-o] [-u] [-t] [-k]\
"
}

guroptions(){
echo "\
-h > show help/info
-o > show options
-u > check usage and create in file
-t > set time intervals for usage records
-k > kill a time interval"
}

timeprompt(){
read -p "How often would you like to record your usage?(in seconds, i.e. 60 = 1 minute) 
\
" timedelay
}

timekill(){
( killall bash )&
}

timedusage(){
    timeprompt
( while [[ $timedelay != "" ]]; do
        usage
        sleep $timedelay
done )&
}

usage() {
date=$(date +%F);
record=$(date +%H:%M:%S);

if [ ! -d "$HOME/usage" ]; then
    mkdir "$HOME/usage";
fi

if [ ! -d "$HOME/usage/$date" ]; then
    mkdir "$HOME/usage/$date";
fi

if [ -f "$HOME/usage/$date/$record.txt" ]; then
    rm "$HOME/usage/$date/$record.txt";
fi

echo $(date) >> $HOME/usage/$date/$record.txt

echo "
\
memory info:_________________________________________________________
\
" >> $HOME/usage/$date/$record.txt
sed -n '1,7p' /proc/meminfo >> "$HOME/usage/$date/$record.txt"
echo "
\

storage space:_______________________________________________________
\
" >> $HOME/usage/$date/$record.txt
df -h | sed -n 1,2p | cut -c 1-10,50-100 >> "$HOME/usage/$date/$record.txt"
echo " 

top procceses:_______________________________________________________
" >> "$HOME/usage/$date/$record.txt"
ps -eo pcpu,pmem,pid,user,args | sort -r -k1 | less | sed -n '1,11p' >> "$HOME/usage/$date/$record.txt"
echo "
\

Network:_____________________________________________________________
\
" >> "$HOME/usage/$date/$record.txt"
/sbin/ifconfig |grep -B1 "inet addr" >> "$HOME/usage/$date/$record.txt"
if [[ $timedelay == "" ]];then
echo "Your usage has been recorded in ~/usage/$date/$record.txt";
fi
}

if [[ $1 == "" ]]; then
    gurhelp
elif [[ $1 == -h ]]; then
    gurhelp
elif [[ $1 == -o ]]; then
    guroptions
elif [[ $1 == -u ]]; then
    usage
elif [[ $1 == -t ]]; then
    timedusage
elif [[ $1 == -k ]]; then
    timekill
else
    gurhelp
fi
